package com.mt.api.modules.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.api.core.page.PageFactory;
import com.mt.api.modules.domain.AppStock;
import com.mt.api.modules.domain.bo.StockCargoPageBo;
import com.mt.api.modules.domain.dto.StockStatQtyByCargoDto;
import com.mt.api.modules.domain.dto.StockWithPrice;
import com.mt.api.modules.manager.IStockManager;
import com.mt.api.modules.mapper.AppStockMapper;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @Author: plover
 * @Date: 2022-01-02
 * @Description:
 */
@Component
public class StockManagerImpl extends ServiceImpl<AppStockMapper, AppStock> implements IStockManager {


    @Override
    public int subtractQtyBySkuId(Integer skuId, Integer qty) {
        return baseMapper.subtractQtyBySkuId(skuId, qty);
    }

    @Override
    public int plusQtyAndTotalQty(Integer id, Integer qty) {
        return baseMapper.plusQtyAndTotalQty(id, qty);
    }

    @Override
    public int plusQtyBySkuId(Integer skuId, Integer qty) {
        return baseMapper.plusQtyBySkuId(skuId, qty);
    }

    @Override
    public int updateQtyById(Integer id, Integer qty) {
        AppStock stock = AppStock.builder().build();
        stock.setId(id);
        stock.setQty(qty);
        return baseMapper.updateById(stock);
    }

    @Override
    public int subtractQtyAndTotalQty(Integer id, Integer qty) {
        return baseMapper.subtractQtyAndTotalQty(id, qty);
    }

    @Override
    public Page<StockStatQtyByCargoDto> getPageGroupByCargo(StockCargoPageBo bo) {
        Page page = PageFactory.defaultPage(bo);
        return baseMapper.getPageGroupByCargo(page, bo);
    }

    @Override
    public AppStock getStockBySkuId(Integer skuId) {
        LambdaQueryWrapper<AppStock> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AppStock::getSkuId, skuId)
                .last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public boolean saveOrUpdateStock(AppStock appStock) {
        AppStock stockBySkuId = getStockBySkuId(appStock.getSkuId());
        if (Objects.isNull(stockBySkuId)) {
            baseMapper.insert(appStock);
        } else {
            baseMapper.plusQtyAndTotalQty(stockBySkuId.getId(), appStock.getQty());
        }
        return true;
    }

    @Override
    public List<AppStock> getListBySkuIds(List<Integer> skuIds) {
        LambdaQueryWrapper<AppStock> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AppStock::getSkuId, skuIds);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<StockWithPrice> getStockWithPrice() {
        return baseMapper.queryStockWithPrice();
    }

    @Override
    public List<StockStatQtyByCargoDto> statQtyByCargo(List<Integer> cargoIdList) {
        return baseMapper.statQtyByCargo(cargoIdList);
    }

    @Override
    public List<AppStock> getStockByCargoIds(List<Integer> cargoIdList) {
        LambdaQueryWrapper<AppStock> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AppStock::getCargoId, cargoIdList);
        return baseMapper.selectList(wrapper);
    }

}
